home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7275 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: hubcap.clemson.edu!hubcap!mjs
  2. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: c++ and readability
  5. Date: 13 Feb 96 23:55:06 GMT
  6. Organization: Clemson University
  7. Message-ID: <mjs.824255706@hubcap>
  8. References: <DMoyuG.CD4@world.std.com> <312097FF.41C67EA6@nada.kth.se> <4fqqrv$np4@solutions.solon.com>
  9. NNTP-Posting-Host: hubcap.clemson.edu
  10.  
  11. seebs@solutions.solon.com (Peter Seebach) writes:
  12.  
  13. >In article <312097FF.41C67EA6@nada.kth.se>,
  14. >Fredrik Rubensson  <prgp-fru@nada.kth.se> wrote:
  15. >>1. Press return before and after {.
  16. >>2. Insert empty lines between code blocks doing different things.
  17. >>(3. Of course good comments increase the readability very much.)
  18.  
  19. >These are both unnecessary and insufficient.  1TBS (The One True Brace Style
  20. >used in K&R, both editions) is an excellent and readable style.
  21.  
  22. >You missed the *crucial* item:
  23. >1.  Indentation should reflect structure.
  24.  
  25. Along these lines, a useful suggestion is:  If a comment refers to a
  26. group of lines, indent the group of lines (by enclosing in a block).
  27.  
  28. Thus, for example:
  29.  
  30.     /* Scan the linked list to see if new_key is already on it.
  31.        If not, insert new node at end of list. */
  32.     {
  33.         list_node *s = NULL, *t = list_head;
  34.  
  35.         while ( t != NULL && t->key != new_key ) {
  36.             s = t;
  37.             t = t->next;
  38.         }
  39.     
  40.         if ( s == NULL ) {
  41.             list_head = new_node(new_key);
  42.             list_head->next = NULL;
  43.         } else {
  44.             t = new_node(new_key);
  45.             t->next = s->next->next;
  46.             s->next = t;
  47.         }
  48.     }
  49.  
  50. Otherwise, there's no really good way to mark the end of the code
  51. being referred to by the comment, especially if you have commented
  52. subblocks within commented blocks.  Also, you can declare variables
  53. needed only for the block inside the block.
  54.  
  55. >Also important:
  56. >2.  Declare functions liberally.  Never write the same code twice.
  57.  
  58. If code should be set off, but is not reused, this technique is often
  59. enough, and you don't have to create long parameter lists.  If you do
  60. discover a need to reuse the code, a block witten this way is almost
  61. ready to be moved directly into a function.
  62.  
  63. >[...]
  64.  
  65. -- 
  66.         Matthew Saltzman
  67.         Clemson University Math Sciences
  68.         mjs@clemson.edu
  69.